package com.hongxia.assetsys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hongxia.assetsys.common.ErrorCode;
import com.hongxia.assetsys.domain.AssertOrg;
import com.hongxia.assetsys.exception.BusinessException;
import com.hongxia.assetsys.service.AssertOrgService;
import com.hongxia.assetsys.mapper.AssertOrgMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
* @author Hongxia
* @description 针对表【tb_assert_org】的数据库操作Service实现
* @createDate 2024-01-06 15:25:20
*/
@Service
public class AssertOrgServiceImpl extends ServiceImpl<AssertOrgMapper, AssertOrg>
    implements AssertOrgService{
    @Resource
    AssertOrgMapper orgMapper;

    @Override
    public Integer addOrg(AssertOrg org) {
        if(org == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        String orgname = org.getOrgname();
        String fatherorg = org.getFatherorg();
        if(orgname == null || orgname.equals("")){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"组织名称为空！");
        }
        if(fatherorg == null || fatherorg.equals("")){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"父类机构为空！");
        }
        boolean result = this.save(org);
        if(!result){
            throw new BusinessException(ErrorCode.SYSTEM_ERROR,"保存失败！");
        }
        return org.getId();
    }

    @Override
    public List<AssertOrg> getOrgListLikely(String name) {
        if(name == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.like("orgname","%" + name + "%");
        List<AssertOrg> orgList = orgMapper.selectList(queryWrapper);
        return orgList;
    }

    @Override
    public AssertOrg getOrgListByName(String name) {
        if(name == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"组织机构名称为空");
        }
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("orgname",name);
        AssertOrg assertOrg = orgMapper.selectOne(queryWrapper);
        return assertOrg;
    }

    @Override
    public List<AssertOrg> getOrgListByFatherOrg(String fatherorg) {
        if(fatherorg == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.like("fatherorg","%" + fatherorg + "%");
        List<AssertOrg> orgList = orgMapper.selectList(queryWrapper);
        return orgList;
    }
}




